package com.kurumi.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kurumi.mapper.WorkStatisticsMapper;
import com.kurumi.query.WorkStatisticsQuery;
import com.kurumi.service.WorkStatisticsService;
import com.kurumi.util.NumberUtils;

@Service
public class WorkStatisticsServiceImpl implements WorkStatisticsService {

	@Autowired
	private WorkStatisticsMapper workStatisticsMapper;
	
	@Override
	public Map<String, Object> pigeonholedStatistics(WorkStatisticsQuery params) {
		// TODO Auto-generated method stub
		Map<String, Object> datas = new HashMap<String, Object>();
		int medicalRecordCount = workStatisticsMapper.getMedicalRecordCountOfShouldPigeonholed(params.getStartDate(), params.getEndDate());
		datas.put("medicalRecordCount", medicalRecordCount);
		
		int imageCountOfShouldPigeonholed = workStatisticsMapper.getImageCountOfShouldPigeonholed(params.getStartDate(), params.getEndDate());
		datas.put("imageCountOfShouldPigeonholed", imageCountOfShouldPigeonholed);
		
		
		int imageCountOfPigeonholed = workStatisticsMapper.getImageCountOfPigeonholed(params.getStartDate(), params.getEndDate());
		datas.put("imageCountOfPigeonholed", imageCountOfPigeonholed);
		if(imageCountOfPigeonholed > 0){
			float pigeonholedRate = imageCountOfPigeonholed/(float)imageCountOfShouldPigeonholed*100;
			datas.put("pigeonholedRate",new DecimalFormat("##0.00").format(pigeonholedRate)+"%");
		}else{
			datas.put("pigeonholedRate","0%");
		}
		
		
		List<Map<String,Object>> groupImageCountOfPigeonholers = workStatisticsMapper.getGroupImageCountOfPigeonholer(params.getStartDate(), params.getEndDate());
		for (Map<String, Object> imageCountOfPigeonholer : groupImageCountOfPigeonholers) {
			long imageCount = (long)imageCountOfPigeonholer.get("count");
			if(imageCount > 0){
				float imageCountRate = new BigDecimal(imageCount).intValue()/(float)imageCountOfShouldPigeonholed*100;
				imageCountOfPigeonholer.put("imageCountRate",new DecimalFormat("##0.00").format(imageCountRate)+"%");
			}else{
				
				imageCountOfPigeonholer.put("imageCountRate","0%");
			}
			
		}
		datas.put("groupImageCountOfPigeonholers", groupImageCountOfPigeonholers);
		return datas;
	}

	@Override
	public Map<String, Object> codingStatistics(WorkStatisticsQuery params) {
		// TODO Auto-generated method stub
		Map<String, Object> datas = new HashMap<String, Object>();
		int medicalRecordCount = workStatisticsMapper.getMedicalRecordCountOfCodinged(params.getStartDate(), params.getEndDate());
		datas.put("medicalRecordCount", medicalRecordCount);
		List<Map<String,Object>> groupMedicalRecordCountOfCodingeds = workStatisticsMapper.getGroupMedicalRecordCountOfCodinged(params.getStartDate(), params.getEndDate());
		for (Map<String, Object> medicalRecordCountOfCodinged : groupMedicalRecordCountOfCodingeds) {
			long codingCount = (long)medicalRecordCountOfCodinged.get("count");
			float codingCountRate = new BigDecimal(codingCount).intValue()/(float)medicalRecordCount*100;
			medicalRecordCountOfCodinged.put("codingCountRate",new DecimalFormat("##0.00").format(codingCountRate)+"%");
		}
		datas.put("groupMedicalRecordCountOfCodingeds", groupMedicalRecordCountOfCodingeds);
		return datas;
	}

	@Override
	public Map<String, Object> pageSignStatistics(WorkStatisticsQuery params) {
		// TODO Auto-generated method stub
		Map<String, Object> datas = new HashMap<String, Object>();
		int medicalRecordCount = workStatisticsMapper.getMedicalRecordCountOfPageSigned(params.getStartDate(), params.getEndDate());
		datas.put("medicalRecordCount", medicalRecordCount);
		List<Map<String,Object>> groupMedicalRecordCountOfSigneds = workStatisticsMapper.getGroupMedicalRecordCountOfPageSigned(params.getStartDate(), params.getEndDate());
		for (Map<String, Object> medicalRecordCountOfSigned : groupMedicalRecordCountOfSigneds) {
			long codingCount = (long)medicalRecordCountOfSigned.get("count");
			BigDecimal signCountRate = NumberUtils.getPrecent(new BigDecimal(codingCount), new BigDecimal(medicalRecordCount));
			
			medicalRecordCountOfSigned.put("signCountRate",NumberUtils.getString(signCountRate, 2)+"%");
		}
		datas.put("groupMedicalRecordCountOfSigneds", groupMedicalRecordCountOfSigneds);
		return datas;
	}

	@Override
	public Map<String, Object> pagePigeonholedStatistics(WorkStatisticsQuery params) {
		// TODO Auto-generated method stub
		Map<String, Object> datas = new HashMap<String, Object>();
		int medicalRecordCount = workStatisticsMapper.getMedicalRecordCountOfPagePigeonholed(params.getStartDate(), params.getEndDate());
		datas.put("medicalRecordCount", medicalRecordCount);
		List<Map<String,Object>> groupMedicalRecordCounts = workStatisticsMapper.getGroupMedicalRecordCountOfPagePigeonholed(params.getStartDate(), params.getEndDate());
		for (Map<String, Object> groupMedicalRecordCount : groupMedicalRecordCounts) {
			long count = (long)groupMedicalRecordCount.get("count");
			BigDecimal countRate = NumberUtils.getPrecent(new BigDecimal(count), new BigDecimal(medicalRecordCount));
			
			groupMedicalRecordCount.put("countRate",NumberUtils.getString(countRate, 2)+"%");
		}
		datas.put("groupMedicalRecordCounts", groupMedicalRecordCounts);
		return datas;
	}

	@Override
	public Map<String, Object> pageQualityControlStatistics(WorkStatisticsQuery params) {
		// TODO Auto-generated method stub
		Map<String, Object> datas = new HashMap<String, Object>();
		int medicalRecordCount = workStatisticsMapper.getMedicalRecordCountOfPageQualityControl(params.getStartDate(), params.getEndDate());
		datas.put("medicalRecordCount", medicalRecordCount);
		List<Map<String,Object>> groupMedicalRecordCounts = workStatisticsMapper.getGroupMedicalRecordCountOfPageQualityControl(params.getStartDate(), params.getEndDate());
		for (Map<String, Object> groupMedicalRecordCount : groupMedicalRecordCounts) {
			long count = (long)groupMedicalRecordCount.get("count");
			BigDecimal countRate = NumberUtils.getPrecent(new BigDecimal(count), new BigDecimal(medicalRecordCount));
			
			groupMedicalRecordCount.put("countRate",NumberUtils.getString(countRate, 2)+"%");
		}
		datas.put("groupMedicalRecordCounts", groupMedicalRecordCounts);
		return datas;
	}

	@Override
	public Map<String, Object> pageCodingStatistics(WorkStatisticsQuery params) {
		// TODO Auto-generated method stub
		Map<String, Object> datas = new HashMap<String, Object>();
		int medicalRecordCount = workStatisticsMapper.getMedicalRecordCountOfPageCoding(params.getStartDate(), params.getEndDate());
		datas.put("medicalRecordCount", medicalRecordCount);
		List<Map<String,Object>> groupMedicalRecordCounts = workStatisticsMapper.getGroupMedicalRecordCountOfPageCoding(params.getStartDate(), params.getEndDate());
		for (Map<String, Object> groupMedicalRecordCount : groupMedicalRecordCounts) {
			long count = (long)groupMedicalRecordCount.get("count");
			BigDecimal countRate = NumberUtils.getPrecent(new BigDecimal(count), new BigDecimal(medicalRecordCount));
			
			groupMedicalRecordCount.put("countRate",NumberUtils.getString(countRate, 2)+"%");
		}
		datas.put("groupMedicalRecordCounts", groupMedicalRecordCounts);
		return datas;
	}

}
